package com.system.service;

import com.system.controller.CommentController;
import com.system.controller.MainUIController;
import com.system.controller.TopicsController;
import com.system.menu.UI;
import com.system.untils.FormatOutput;
import com.system.untils.SensitiveWordsInspect;

import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Scanner;


public class CommentService {
    private final Scanner sc;
    private final TopicsController tc;
    private final CommentController controller;
    private final MainUIController muc;
    private final UI ui = new UI();
    private final SensitiveWordsInspect swi = new SensitiveWordsInspect();
    private final FormatOutput fo = new FormatOutput();
    private final ReplyComment rc;

    public CommentService() {
        this.tc = new TopicsController();
        this.controller = new CommentController();
        this.muc = new MainUIController();
        this.rc = new ReplyComment();
        this.sc = new Scanner(System.in);
    }


    public void ShowAllTopicsMenu(String email) {
        // 显示第一页内容并显示当前页和总页数
        int currentPage = 1;
        int totalPages = getTotalPages();
        while (true) {
            this.ui.HeadLine();
            this.tc.getTdm().selectAllTopics(currentPage);
            // 显示当前页和总页数
            System.out.println(" \t\t\t\t\t\t\t当前页：" + currentPage + "  共 " + totalPages + " 页");
            this.ui.CommentUI();
            System.out.println("请输入你的选择：");
            try {
                int optAllTopics = sc.nextInt();
                switch (optAllTopics) {
                    case 1:
                        System.out.println("请输入你选择的帖子：");
                        int optId = sc.nextInt();
                        optionTopic(email, optId);
                        break;
                    case 2:
                        // 查看下一页
                        if (currentPage < totalPages) {
                            currentPage++;
                            System.out.println();
                            this.ui.HeadLine();
                            this.tc.getTdm().selectAllTopics(currentPage);
                            System.out.println(" \t\t\t\t\t\t\t当前页：" + currentPage + "  共 " + totalPages + " 页");
                        } else {
                            System.out.println();
                            this.ui.HeadLine();
                            this.tc.getTdm().selectAllTopics(currentPage);
                            System.out.println(" \t\t\t\t\t\t\t当前页：" + currentPage + "  共 " + totalPages + " 页");
                            System.out.println("已经是最后一页！");
                        }
                        break;
                    case 3:
                        // 查看上一页
                        if (currentPage > 1) {
                            currentPage--;
                            System.out.println();
                            this.ui.HeadLine();
                            this.tc.getTdm().selectAllTopics(currentPage);
                            System.out.println(" \t\t\t\t\t\t\t当前页：" + currentPage + "  共 " + totalPages + " 页");
                        } else {
                            System.out.println();
                            this.ui.HeadLine();
                            this.tc.getTdm().selectAllTopics(currentPage);
                            System.out.println(" \t\t\t\t\t\t\t当前页：" + currentPage + "  共 " + totalPages + " 页");
                            System.out.println("已经是第一页！");
                        }
                        break;
                    case 4:
                        return;
                    default:
                        System.out.println("输入有误，请重新选择！");
                        break;
                }
            }catch(InputMismatchException e) {
                System.out.println("输入错误，请输入数字！");
                sc.next(); // 清除错误输入
            }
        }
    }

    // 获取总页数--帖子
    public int getTotalPages() {
        int totalRecords = this.tc.getTdm().getTotalRecords();  // 获取总记录数
        int pageSize = 5; // 每页显示5条
        return (int) Math.ceil((double) totalRecords / pageSize); // 向上取整得到总页数
    }

    // 帖子对应的评论
    public int getTotalPagesComments(int publish_id) {
        int totalRecords = this.controller.getCdm().getTotalComments(publish_id);  // 获取总记录数
        int pageSize = 5; // 每页显示5条
        return (int) Math.floor((double) totalRecords / pageSize); // 向上取整得到总页数
    }


    public void showOptionTopic(int id) {
        System.out.println("               lake of letters         ");
        System.out.println("------------------------------------------------------------");
        this.controller.getCdm().showOption(id);  // 展示对应id的帖子
    }
    public void showOptionTopicForNotion(int id) {
        System.out.println("               lake of letters         ");
        System.out.println("------------------------------------------------------------");
        this.controller.getCdm().showNotion(id);  // 展示对应id的帖子
    }

    public void optionTopic(String email, int id) {
        while (true) {
            System.out.println("               lake of letters         ");
            System.out.println("------------------------------------------------------------");
            this.ui.showOnlyTopic(muc.getMdm().getName(email));
            this.controller.getCdm().showOption(id);  // 展示对应id的帖子
            System.out.println("请输入你的选择：");
            try {
                int optFunction = sc.nextInt();
                switch (optFunction) {
                    case 1:
                        publish(email, id);
                        break;
                    case 2:
                        HashMap<Integer, Integer> map = this.controller.getCdm().loadAllTopics();
                        showCommentDeepZero(email, id, map.get(id));
                        break;
                    case 3:
                        return;
                    default:
                        System.out.println("输入有误！");
                        optionTopic(email, id);
                        break;
                }
            }catch(InputMismatchException e) {
                System.out.println("输入错误，请输入数字！");
                sc.next(); // 清除错误输入
            }
        }
    }

    // 发表评论（发表评论的深度一定是在0）
    public void publish(String email, int id) {
        sc.nextLine(); // 清除换行符
        boolean isPublished = false;
        while (!isPublished) {
            System.out.println("请输入回复内容(连续回车继续输入，输入END结束):");
            try {

                String commitInput = this.fo.multiLineInput();
                if (this.swi.inspectText(commitInput)) {
                    System.out.println("评论内容含有敏感内容！(回车继续输入)");
                } else {
                    this.controller.getCdm().addCommentText(email, commitInput, id);
                    System.out.println("评论成功！");
                    isPublished = true;
                }
            }catch(InputMismatchException e) {
                System.out.println("输入错误，请输入数字！");
                sc.next(); // 清除错误输入
            }
        }
    }

public void showCommentDeepZero(String email, int inputId, int commentID) {
    if (this.controller.getCdm().getTotalComments(commentID) == 0) {
        System.out.println("该帖子下没有评论！");
        return;
    }
    int currentPage = 1;
    int totalPages = getTotalPagesComments(commentID);
    while (true) {
        showOptionTopicForNotion(inputId);
        this.controller.getCdm().selectAllCommentDeep(currentPage, commentID);
        // 显示当前页和总页数
        System.out.println(" \t\t\t\t\t\t\t当前页：" + currentPage + "  共 " + totalPages + " 页");
        this.ui.showCommentDeep();
        System.out.println("请输入你的选择：");
        try {
            int optAllTopics = sc.nextInt();
            switch (optAllTopics) {
                case 1:
                    if (!replyComment(email, commentID)) {
                        showCommentDeepZero(email, inputId, commentID);
                    } else {
                        break;
                    }
                case 2:
                    // 查看下一页
                    if (currentPage < totalPages) {
                        currentPage++;
                        refreshPage(inputId, commentID, currentPage); // 传递
                    } else {
                        System.out.println("已经是最后一页！");
                    }
                    break;
                case 3:
                    // 查看上一页
                    if (currentPage > 1) {
                        currentPage--;
                        refreshPage(inputId, commentID, currentPage); // 传递
                    } else {
                        System.out.println("已经是第一页！");
                    }
                    break;
                case 4:
                    return;
                default:
                    System.out.println("输入有误，请重新选择！");
                    break;
            }
        }catch(InputMismatchException e) {
            System.out.println("输入错误，请输入数字！");
            sc.next(); // 清除错误输入
        }
    }
}
    private void refreshPage(int inputId, int commentID, int currentPage) {
        showOptionTopic(inputId);
        this.controller.getCdm().selectAllCommentDeep(currentPage, commentID);
        int totalPages = getTotalPagesComments(commentID); // 更新总页数
        System.out.println(" \t\t\t\t\t\t\t当前页：" + currentPage + "  共 " + totalPages + " 页");
    }
    private void refreshPageForNotion(int commentID, int currentPage) {
        showOptionTopicForNotion(commentID);
        this.controller.getCdm().selectAllCommentDeep(currentPage, commentID);
        int totalPages = getTotalPagesComments(commentID); // 更新总页数
        System.out.println(" \t\t\t\t\t\t\t当前页：" + currentPage + "  共 " + totalPages + " 页");
    }

    // 选择帖子
    public boolean optionTopicID(int commentID,int inputID) {
        HashMap<Integer, Integer> map = this.controller.getCdm().getCommentID(commentID);
        return map.containsKey(inputID);
    }
    // 选择评论回复
    public boolean replyComment(String email,int commentID) {
        System.out.println("请输入序号：");
        try {
            int optionReplyID = sc.nextInt();
            // 检查输入的序号是否超出范围
            if (this.controller.getCdm().getTotalComments(commentID) == 0) {
                System.out.println("该评论下没有回复,是否重新选择?(y/n)");
                String reOption = sc.next();
                if ("y".equalsIgnoreCase(reOption) || "Y".equalsIgnoreCase(reOption)) {
                    replyComment(email, commentID); // 重新选择
                } else {
                    return false; // 退出方法
                }
            }
            if (optionReplyID > this.controller.getCdm().getTotalComments(commentID)) {
                System.out.println("输入错误，序号超出范围！");
                replyComment(email, commentID); // 重新输入
            } else if (optionTopicID(commentID, optionReplyID)) {
                // 存在且找到了，跳转
                HashMap<Integer, Integer> map = this.controller.getCdm().getCommentID(commentID);
                int parentCommentID = map.get(optionReplyID); // 拿到了对应的
                this.rc.replyCommentMenu(email, parentCommentID, commentID);
                return true;
            }
        }catch(InputMismatchException e) {
            System.out.println("输入错误，请输入数字！");
            sc.next(); // 清除错误输入
        }
        return false;
    }
    public void showCommentDeepZeroUtil(String email,int commentID) {
//        if (this.controller.getCdm().getTotalComments(commentID) == 0) {
//            System.out.println("该帖子下没有评论！");
//            return;
//        }
        int currentPage = 1;
        int totalPages = getTotalPagesComments(commentID);
        while (true) {
            showOptionTopicForNotion(commentID);
            this.controller.getCdm().selectAllCommentDeep(currentPage, commentID);
            // 显示当前页和总页数
            System.out.println(" \t\t\t\t\t\t\t当前页：" + currentPage + "  共 " + totalPages + " 页");
            this.ui.showCommentDeep();
            System.out.println("请输入你的选择：");
            try {
                int optAllTopics = sc.nextInt();
                switch (optAllTopics) {
                    case 1:
                        if (!replyComment(email, commentID)) {
                            showCommentDeepZeroUtil(email,commentID);
                        } else {
                            break;
                        }
                    case 2:
                        // 查看下一页
                        if (currentPage < totalPages) {
                            currentPage++;
                            refreshPageForNotion(commentID,currentPage);
                        } else {
                            System.out.println("已经是最后一页！");
                        }
                        break;
                    case 3:
                        // 查看上一页
                        if (currentPage > 1) {
                            currentPage--;
                            refreshPageForNotion(commentID,currentPage);
                        } else {
                            System.out.println("已经是第一页！");
                        }
                        break;
                    case 4:
                        return;
                    default:
                        System.out.println("输入有误，请重新选择！");
                        break;
                }
            }catch(InputMismatchException e) {
                System.out.println("输入错误，请输入数字！");
                sc.next(); // 清除错误输入
            }
        }
    }
}
